//
//  ChooseTaskViewController.m
//  leo
//
//  Created by 常艳 on 15/12/14.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "ChooseTaskViewController.h"
#import "GrowDataManager.h"
#import "ChooseTaskTableViewCell.h"

@interface ChooseTaskViewController ()
@property(nonatomic,strong) GrowDataManager *growDataManager;
@property(nonatomic,strong) NSArray *taskArray;
@property(nonatomic,assign) float rate;
@property(nonatomic,assign) BOOL isRegis;
@property(nonatomic,strong) ChooseTaskTableViewCell *selectCell;

@end

@implementation ChooseTaskViewController
@synthesize growDataManager;
@synthesize taskArray;
@synthesize rate;
@synthesize isRegis;
@synthesize selectCell;
@synthesize assginTaskView;
-(void)dealloc
{
    growDataManager=NULL;
    taskArray=NULL;
    assginTaskView=NULL;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"选择任务";
    CGRect r=[ UIScreen mainScreen ].applicationFrame;
    rate=r.size.width/320;
    growDataManager=[[GrowDataManager alloc] init];
    if(growDataManager==NULL)
        return;
    taskArray=[growDataManager getSysTasks];
    if(taskArray==NULL)
        return;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor=[UIColor clearColor];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(taskArray!=NULL)
        return [taskArray count];
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"ChooseTaskTableViewCell";
    NSInteger row=[indexPath row];
    if(!isRegis)
    {
        [tableView registerNib:[UINib nibWithNibName:@"ChooseTaskTableViewCell" bundle:nil] forCellReuseIdentifier: @"ChooseTaskTableViewCell"];
        isRegis=YES;
    }
    Task *task=[taskArray objectAtIndex:row];
    if(task==NULL)
    {
        UITableViewCell *cell=[[UITableViewCell alloc] init];
        return cell;
    }
    ChooseTaskTableViewCell *cell = (ChooseTaskTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.selectImg.hidden=YES;
    cell.titleLable.text=task.title;
    cell.backgroundColor=[UIColor whiteColor];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectCell!=NULL)
        selectCell.selectImg.hidden=YES;
    ChooseTaskTableViewCell *cell=(ChooseTaskTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if(cell!=NULL)
    {
        cell.selectImg.hidden=NO;
        selectCell=cell;
    }
    Task *task=[taskArray objectAtIndex:[indexPath row]];
    if(assginTaskView!=NULL)
        assginTaskView.task=task;
    
}

@end
